package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.crm.mapper.GwasTMapper;
import cn.e4j.modular.crm.service.GwasTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-24
 */
@Service
public class GwasTServiceImpl extends ServiceImpl<GwasTMapper, GwasT> implements GwasTService {

    /**  以下为配件仓库维护作业base/gwai600 **/

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getOrgsPageList(Dto dto, Page<Object> page) {
        return this.baseMapper.getOrgsList(dto, page);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getOrgsList(Dto dto) {
        return this.baseMapper.getOrgsList(dto);
    }

    @Transactional
    @Override
    public void add1(GwasT gwasT, GwaslT gwaslT) {
        ShiroUser shiroUser = ShiroKit.getUser();

        gwasT.setGWASENT(shiroUser.getDBC());
        gwasT.setGWASDATE(LocalDate.now());
        gwasT.setGWASTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwasT.setGWASUSER(shiroUser.getACCOUNT());
        gwasT.setGWASGRUP(shiroUser.getORG_ID());
        gwasT.setGWASORIU(shiroUser.getACCOUNT());
        gwasT.setGWASORIG(shiroUser.getORG_ID());

        gwaslT.setGWASLENT(shiroUser.getDBC());
        gwaslT.setGWASL001(gwasT.getGWAS001());
        gwaslT.setGWASL002(LangUtils.getLang(shiroUser.getLANG()));

        gwasT.insert();
        gwaslT.insert();
    }
    @Transactional
    @Override
    public void edit1(GwasT gwasT, GwaslT gwaslT) {
        ShiroUser shiroUser = ShiroKit.getUser();

        gwasT.setGWASMODU(shiroUser.getACCOUNT());

        UpdateWrapper<GwasT> gwasTUpdateWrapper = new UpdateWrapper<>();
        gwasTUpdateWrapper.eq("GWASENT", gwasT.getGWASENT());
        gwasTUpdateWrapper.eq("GWAS001", gwasT.getGWAS001());

        UpdateWrapper<GwaslT> gwaslTUpdateWrapper = new UpdateWrapper<>();
        gwaslTUpdateWrapper.eq("GWASLENT", gwasT.getGWASENT());
        gwaslTUpdateWrapper.eq("GWASL001", gwasT.getGWAS001());


        gwasT.update(gwasTUpdateWrapper);
        gwaslT.update(gwaslTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRow(GwasT gwasT, GwaslT gwaslT) {
        UpdateWrapper<GwasT> gwasTUpdateWrapper = new UpdateWrapper<>();
        gwasTUpdateWrapper.eq("GWASENT", gwasT.getGWASENT());
        gwasTUpdateWrapper.eq("GWAS001", gwasT.getGWAS001());

        UpdateWrapper<GwaslT> gwaslTUpdateWrapper = new UpdateWrapper<>();
        gwaslTUpdateWrapper.eq("GWASLENT", gwasT.getGWASENT());
        gwaslTUpdateWrapper.eq("GWASL001", gwasT.getGWAS001());


        gwasT.delete(gwasTUpdateWrapper);
        gwaslT.delete(gwaslTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<GwasT> gwasTList) {
        for (GwasT gwasT :gwasTList) {
            UpdateWrapper<GwasT> gwasTUpdateWrapper = new UpdateWrapper<>();
            gwasTUpdateWrapper.eq("GWASENT", gwasT.getGWASENT());
            gwasTUpdateWrapper.eq("GWAS001", gwasT.getGWAS001());


            GwaslT gwaslT=new GwaslT();
            UpdateWrapper<GwaslT> gwaslTUpdateWrapper = new UpdateWrapper<>();
            gwaslTUpdateWrapper.eq("GWASLENT", gwasT.getGWASENT());
            gwaslTUpdateWrapper.eq("GWASL001", gwasT.getGWAS001());


            gwasT.delete(gwasTUpdateWrapper);
            gwaslT.delete(gwaslTUpdateWrapper);
        }
    }
    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(GwasT gwasT) {
        QueryWrapper<GwasT> gwasTQueryWrapper = new QueryWrapper<>();
        gwasTQueryWrapper.eq("GWASENT", gwasT.getGWASENT());
        gwasTQueryWrapper.eq("GWAS001", gwasT.getGWAS001());
        return this.baseMapper.selectList(gwasTQueryWrapper).size() > 0;
    }
}
